package
{
	
	
	import com.adobe.ac.mxeffects.DistortionConstants;
	import com.adobe.ac.mxeffects.Flip;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObjectContainer;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.SharedObject;
	
	import mx.containers.Canvas;
	import mx.controls.Alert;
	import mx.events.CloseEvent;
	import mx.events.EffectEvent;

	public class Authorization extends Canvas
	{
		public var parentWindow:LoginInfo;
		public var myParent:DisplayObjectContainer;
		//private var So:SharedObject= SharedObject.getLocal("TempImage");
		//[Bindable]
		//private var savedBitmap:Bitmap;
		private var user:UserSpace;
		private var admin:AdminSpace;
		
		public function Authorization()
		{
			super();
		}
		
		private function flipToUser() : void
		{
			var e : Flip = new Flip( parentWindow );
			user = new UserSpace();
			e.siblings = [ user ];
			e.direction = DistortionConstants.RIGHT;
			e.duration = 1000;
			e.addEventListener( EffectEvent.EFFECT_END, onToUserEffect);
			e.play();
		}
		
		private function onToUserEffect( event : EffectEvent ) : void
		{
			if (this.parent != null)
			{
				myParent=this.parent;
				myParent.removeChild(parentWindow);
			}
			myParent.addChild(user);
		}
		
		private function flipToAdmin() : void
		{
			var e : Flip = new Flip( parentWindow );
			admin = new AdminSpace();
			e.siblings = [ admin ];
			e.direction = DistortionConstants.RIGHT;
			e.duration = 1000;
			e.addEventListener( EffectEvent.EFFECT_END, onToAdminEffect );
			e.play();
		}
		
		private function onToAdminEffect( event : EffectEvent ) : void
		{
			if (this.parent != null)
			{
				myParent=this.parent;
				myParent.removeChild(parentWindow);
			}
			myParent.addChild(admin);
		}

		private function selectMode(event:CloseEvent) : void
		{
			if (event.detail == Alert.YES)
			{
				//admin mode	
				flipToAdmin();
			}
			else if (event.detail == Alert.NO)
			{
				//user mode
				flipToUser();
			}
				
		}
		
		private function nextPage(event:CloseEvent) : void
		{
			flipToUser();
		}
		
		public function onLoginComplete(event:AuthorizationEvent):void{
			
		/*	var imgLoader:Loader = new Loader();
			imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,displayImage);
			imgLoader.loadBytes(So.data.image);
		*/
			var flag:int=event.isAdmin;
			
			if(flag==0){//usermode
				
				Alert.okLabel = "NEXT";
				
				Alert.show("PASSWORD AUTHENTICATION COMPLETED","Success", Alert.OK, this, nextPage);
			
			}else{	
				Alert.yesLabel = "ADMIN";
				Alert.noLabel = "USER";
						
				Alert.show("Which Mode Would You Like to Continue With?","MODE SELECTION", Alert.YES | Alert.NO, this, selectMode);

			}
		}
		
		/*public function displayImage(event:Event):void {
		
			savedBitmap = event.currentTarget.content as Bitmap;
		}*/
		
	}
}